﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MainApp
{
    public partial class Dispenser : DevExpress.XtraEditors.XtraUserControl
    {
        private Guid id;

        public Dispenser()
        {
            InitializeComponent();
        }

        public Dispenser(Guid id, string name, decimal oldValue, decimal newValue, bool today, bool canEdit)
            : this()
        {
            groupControl.Text = name;
            this.id = id;
            spOldValue.EditValue = oldValue;            
            spNewValue.Validating += new CancelEventHandler(spNewValue_Validating);
            //spNewValue.Validated += new EventHandler(spNewValue_Validated);                        
            //spNewValue.Properties.MaxValue = 9999999999999;
            spNewValue.EditValue = newValue;
            spNewValue.Properties.ReadOnly = !canEdit;
            btnDelete.Enabled = today && canEdit;
            if (btnDelete.Enabled)
            {
                btnDelete.Click += new EventHandler(btnDelete_Click);
                groupControl.DoubleClick += new EventHandler(groupControl_DoubleClick);                
            }
            spNewValue.EditValueChanged += spNewValue_ValueChanged;
        }

        void groupControl_DoubleClick(object sender, EventArgs e)
        {
            FrmGetName tmp = new FrmGetName(groupControl.Text);
            if (tmp.ShowDialog() == DialogResult.OK)
            {
                groupControl.Text = tmp.GetName();
                ChangedName = true;
                this.ValueChanged.Invoke(sender, e);
            }
        }

        public event EventHandler ValueChanged;
        public event EventHandler Deleted;
        
        void btnDelete_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show(String.Format("Xóa vòi {0} ?", groupControl.Text), "Xác nhận", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.Deleted.Invoke(this, e);
            }
        }

        void spNewValue_Validated(object sender, EventArgs e)
        {            
            this.ValueChanged.Invoke(sender, e);
        }

        void spNewValue_ValueChanged(object sender, EventArgs e)
        {
            this.ValueChanged.Invoke(sender, e);
        }

        void spNewValue_Validating(object sender, CancelEventArgs e)
        {
            if ((decimal)spNewValue.EditValue < spOldValue.Value)
            {
                e.Cancel = true;
            }
        }

        public decimal TotalInput
        {
            get
            {
                return (decimal)spNewValue.EditValue - spOldValue.Value;
            }
        }

        public decimal OldValue { get { return spOldValue.Value; } }        
        public Guid ID { get { return id; } }
        public string DispenserName { get { return groupControl.Text; } }
        public bool ChangedName { get; set; }
    }
}
